<?php
/*
 * $Id$
 * Copyright (c) 2010 Craig Watson [ craig@cwatson.org ]
 * Distributed Under the Mozilla Public License 1.1 [ http://www.mozilla.org/MPL/MPL-1.1.html ]
 */
if (!defined('IN_OB')){
	exit;
}

$content = $progress = "";
$content .= "<h2>OpenBooking Installation Progress</h2>\n<div class=\"mono\">";

if(isset($_POST['submit'])){

    // If the details haven't been validated or the validation has failed, display error    
    if (((!isset($_POST['ldap_valid'])) || (!isset($_POST['db_valid']))) || ((!$_POST['ldap_valid']) || (!$_POST['db_valid']))){
        $content .= "<p>Your details have not been validated. Please <a href=\"index.php\">go back</a> and re-complete the form, taking care to validate your credentials for <strong>both</strong> servers.</p>";
    } else {
    
        /** INSTALL STAGE 0 -- Parse Data **/
        $dbhost   = clean_string($_POST['db_hostname']);
        $dbuser   = clean_string($_POST['db_username']);
        $dbpass   = addslashes(trim($_POST['db_password']));
        $dbname   = clean_string($_POST['db_name']);
        $dbprefix = clean_string($_POST['db_table_prefix']);  
        $dbms     = clean_string($_POST['db_dbms']);      
        
        $ldaphost     = clean_string($_POST['ldap_hostname']);
        $ldap_base_dn = clean_string($_POST['ldap_base_dn']);
        $ldap_admins  = clean_string($_POST['ldap_admin_users']);
        $ldap_cn      = clean_string($_POST['ldap_user_cn']);
        
        /** INSTALL STAGE 1 -- Database **/
        
        // Set up the connection and require the schema file
        require_once $root_path . 'includes/adodb5/adodb.inc.' . $phpExt; 
        $db = NewADOConnection($dbms);
        $db->Connect($dbhost, $dbuser, $dbpass, $dbname);
        $dict = NewDataDictionary($db);
        require_once "./includes/schema.php";
        $table_options =  array('mysql' => 'ENGINE=MyISAM', 'oci8' => 'tablespace users', 'REPLACE');

        // Generate and execute the SQL
        foreach($schema as $table => $entry){
            $table_name = $dbprefix . $table;
            $dict->ExecuteSQLArray($dict->CreateTableSQL($table_name, $entry['fields'],$table_options));
            $progress .= "<p>Created table $table_name ...</p>\n";
        }
        
        // Create the "No Parent" location
        $db->Execute("INSERT INTO $dbprefix" . "locations (location_id, loc_name, loc_type, loc_order_hash, loc_num_children, loc_parent_id) VALUES (0, 'No Parent', 0, '000-000-000-000', 0, 0)");       
        $db->Execute("UPDATE $dbprefix" . "locations SET location_id = 0 WHERE location_id = 1");
        $db->Execute("ALTER TABLE $dbprefix" . "locations AUTO_INCREMENT = 1");    
        $progress .= "<p>Created base \"No Parent\" location ...</p>\n";
        
        /** INSTALL STAGE 2 -- Write values to config.php **/
        $config = "<?php\n/*\n * OpenBooking config.php File\n * Generated on " . @date("c") . "\n * DO NOT EDIT THIS FILE!\n */\n\n";
        
        // Output database info
        $config .= '// Database' . "\n" . '$dbms = "' . $dbms . '"; $dbhost = "' . $dbhost . '"; $dbuser = "' . $dbuser . '"; $dbpass = "' . $dbpass . '"; $dbname = "' . $dbname . '"; $table_prefix = "' . $dbprefix . '";' . "\n\n"; 
        
        // Output LDAP connection info, then explode and loop through the admins list
        $config .= '// LDAP' . "\n" . '$ldap_server = "' . $ldaphost . '"; $ldap_base_DN = "' . $ldap_base_dn . '"; $ldap_user_cn = "' . $ldap_cn . '"; $admin_users = array(';
        $admins_array = explode(",", $ldap_admins);
        foreach($admins_array as $key => $admin){
            $config .= '"' . $admin . '"';
            if ($key != count($admins_array)-1){
                $config .= ',';
            }
        }
        $config .= ");\n\n";
        
        // Close config content, write to file and close file, then unset the content for security
        $config .= '// Other configuration' . "\n" . 'define("OB_INSTALLED",TRUE);' . "\n\n?>";
        $file = fopen("../includes/config.php","w");
        fwrite($file,$config);
        fclose($file);
        unset($config);
        $progress .= "<p>Written config.php file ...</p>\n";


        // Put the progress in a <div>
        $content .= $progress . "</div>\n\n";
        $content .= "<h4>Installation Successful!</h4>\n<p><strong>Thank you, you have successfully installed OpenBooking.</strong></p>\n<p>Please delete the \"install\" directory, then proceed to the <a href=\"../login.php\">Login</a> page to continue setting up the system.</p>";
        
    }
    
    

} else {
    $content .= "<p>The form was not submitted.</p>";
}

echo $content;

?>


